﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.Models
{
    [MetadataType(typeof(ClubOtherFixtureDetailMetaData))]
    public partial class ClubOtherFixtureDetail
    {
        CricketClubEntities db = new CricketClubEntities();
        ClubFixturePlayer clubFixturePlayer = new ClubFixturePlayer();
        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();

        public List<string> ValidateClubOtherDetails(ClubOtherFixtureDetail clubOtherDetail)
        {
            string message;

            List<string> errorMessages = new List<string>();

            if (clubOtherDetail.byes < 0)
            {
                message = "byes must be greater than or equal to zero";
                errorMessages.Add(message);
            }

            if (clubOtherDetail.leg_byes < 0)
            {
                message = "leg byes must be greater than or equal to zero";
                errorMessages.Add(message);
            }

            if (clubOtherDetail.wides < 0)
            {
                message = "wides must be greater than or equal zero";
                errorMessages.Add(message);
            }

            if (clubOtherDetail.no_balls < 0)
            {
                message = "no-balls must be greater than or equal to zero";
                errorMessages.Add(message);
            }

            return errorMessages;
        }

        public double GetTotalExtras(ClubOtherFixtureDetail clubOtherDetail)
        {
            double totalExtras = (double)clubOtherDetail.byes + (double)clubOtherDetail.leg_byes + (double)clubOtherDetail.wides + (double)clubOtherDetail.no_balls;

            return totalExtras;
        }

        public void UpdateClubOtherDetails(ClubOtherFixtureDetail clubOtherDetail, List<ClubFixturePlayer> clubBatsmen)
        {
            ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail();

            clubOtherFixtureDetail.fixture_id = clubOtherDetail.fixture_id;
            clubOtherFixtureDetail.byes = clubOtherDetail.byes;
            clubOtherFixtureDetail.leg_byes = clubOtherDetail.leg_byes;
            clubOtherFixtureDetail.wides = clubOtherDetail.wides;
            clubOtherFixtureDetail.no_balls = clubOtherDetail.no_balls;
            clubOtherFixtureDetail.total_runs = clubFixturePlayer.GetTotalBatsmenRuns(clubBatsmen) + GetTotalExtras(clubOtherDetail);
            clubOtherFixtureDetail.wickets_lost = clubFixturePlayer.GetTotalWicketsLost(clubBatsmen);

            db.ClubOtherFixtureDetails.AddObject(clubOtherFixtureDetail);
            db.SaveChanges();
        }

        public void EditClubOtherFixtureDetail(ClubOtherFixtureDetail otherDetail)
        {
            ClubOtherFixtureDetail detailToEdit = db.ClubOtherFixtureDetails.Where(cfd => cfd.fixture_id == otherDetail.fixture_id).Single();

            detailToEdit.byes = otherDetail.byes;
            detailToEdit.leg_byes = otherDetail.leg_byes;
            detailToEdit.wides = otherDetail.wides;
            detailToEdit.no_balls = otherDetail.no_balls;

            List<ClubFixturePlayer> fixturePlayers = clubFixturePlayerRepository.GetClubFixturePlayers(otherDetail.fixture_id).ToList();
            detailToEdit.total_runs = clubFixturePlayerRepository.GetTotalBattingRuns(fixturePlayers) + detailToEdit.byes + detailToEdit.leg_byes + detailToEdit.wides + detailToEdit.no_balls;
            detailToEdit.wickets_lost = clubFixturePlayerRepository.GetTotalWickets(fixturePlayers);
            
            db.ObjectStateManager.ChangeObjectState(detailToEdit, EntityState.Modified);
            db.SaveChanges();
        }

        public void DeleteClubOtherFixtureDetails(int Id)
        {
            ClubOtherFixtureDetail otherDetails = db.ClubOtherFixtureDetails.Where(cofd => cofd.fixture_id == Id).Single();

            db.ClubOtherFixtureDetails.DeleteObject(otherDetails);
            db.SaveChanges();
        }

    }

    public class ClubOtherFixtureDetailMetaData
    {
        [Display(Name = "Byes")]
        public double byes;

        [Display(Name = "Leg Byes")]
        public double leg_byes;

        [Display(Name = "Wides")]
        public double wides;

        [Display(Name = "No Balls")]
        public double no_balls;
    }
}